package com.kotlin.android.app.provider

import android.app.Activity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.app.router.path.RouterActivityPath
import com.android.app.router.path.RouterProviderPath
import com.android.app.router.provider.IAppProvider
import com.kotlin.android.app.MainTabActivity
import com.kotlin.android.app.ad.ADManager
import com.kotlin.android.app.data.common.FlashAd
import com.kotlin.android.app.data.common.TheatersBoxAd
import com.kotlin.android.core.entity.PAGE_FLAG
import com.kotlin.android.core.entity.PageFlag
import com.kotlin.android.ktx.lifecycle.KtxActivityManager
import com.kotlin.android.router.RouterManager

/**
 *
 */
@Route(path = RouterProviderPath.Provider.PROVIDER_APP)
class AppProvider : IAppProvider {

    /**
     * Activity栈中是否有主页
     */
    override val hasMainPage: Boolean
        get() = KtxActivityManager.containsByName(MainTabActivity::class.java.name)

    /**
     * 跳转app Tab主页，[flag] 页面标示。
     */
    override fun startMainTabPage(
        context: Activity?,
        flag: PageFlag?,
        needCloseCurrentActivity: Boolean
    ) {
        Bundle().apply {
            putParcelable(PAGE_FLAG, flag)
        }.also {
            RouterManager.instance.navigation(
                context = context,
                path = RouterActivityPath.App.PAGER_APP,
                bundle = it,
                needCloseCurrentActivity = needCloseCurrentActivity
            )
        }
    }

    /**
     * 清除广告
     *
     * 闪屏广告（1），热映广告（2）
     */
    override fun clearAD(type: Int) {
        when (type) {
            1 -> ADManager.instance.saveAD<FlashAd>(null)
            2 -> ADManager.instance.saveAD<TheatersBoxAd>(null)
        }
    }
}